package com.ohuang.download;

public abstract class PreDownLoadTask extends DownloadTask implements Runnable {
    protected int taskSate = DownloadTask.SATE_NEW;

    protected RemoteFile remoteFile;
    private final PreDownloadListener preDownloadListener;

    public PreDownLoadTask(OhDownLoad ohDownLoad, String url, PreDownloadListener preDownloadListener) {
        super(ohDownLoad, url);
        this.preDownloadListener = preDownloadListener;
    }


    protected Exception error;

    @Override
    public void run() {
        if (taskSate == DownloadTask.SATE_NEW) {
            preDownloadListener.onStart(this);
            taskSate = DownloadTask.SATE_RUN;
            predownload(url);
        }
        taskSate = DownloadTask.SATE_STOP;
        ohDownLoad.downloadPreTaskStop(this);
        preDownloadListener.onStop(this);
        if (error != null) {
            preDownloadListener.onError(this, error, remoteFile);
            error = null;
        }
    }

    protected void onSuccess() {
        preDownloadListener.onSuccess(this, remoteFile);
    }

    protected void setRemoteFile(RemoteFile remoteFile) {
        this.remoteFile = remoteFile;
    }

    protected void setError(Exception error) {
        this.error = error;
    }

    protected abstract void predownload(String url);


}
